<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="500px" :minHeight="330" destroyOnClose>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
import {computed} from 'vue';
import {BasicModal, useModalInner} from '/@/components/Modal';
import {BasicForm, useForm} from '/@/components/Form/index';
import {downloadFormSchema} from './bill.download.data';
import {downloadBill} from './bill.download.api';
import {useMessage} from '/@/hooks/web/useMessage';

const { createMessage } = useMessage();
// 声明Emits
const emit = defineEmits(['register', 'success']);
//表单配置
const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  schemas: downloadFormSchema,
  showActionButtonGroup: false
});
//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //重置表单
  await resetFields();
  setModalProps({ confirmLoading: false });
});
//设置标题
const title = computed(() => '下载账单');
//表单提交事件
async function handleSubmit(v) {
  try {
    let values = await validate();
    setModalProps({ confirmLoading: true });
    await downloadBill(values)
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success');
    createMessage.success(`账单开始下载中...`)
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
